<template>
  <div>
    <div>
      <el-form :inline="true" :model="searchObj" class="demo-form-inline">
        <el-form-item label="标签分组">
          <el-select v-model="searchObj.labelId" placeholder="请选择标签分组">
            <el-option
              v-for="item in options"
              :key="item.labelId"
              :label="item.labelName"
              :value="item.labelId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称">
          <el-input v-model="searchObj.tagName" placeholder="标签名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <!-- 添加标签 -->
      <el-button type="text" @click="dialogFormVisible = true">添加标签</el-button>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="tag">
          <el-form-item label="标签名称" :label-width="formLabelWidth">
            <el-input v-model="tag.tagName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="标签分组" :label-width="formLabelWidth">
            <el-select v-model="tag.labelId" placeholder="请选择标签分组">
              <el-option
                v-for="item in options"
                :key="item.labelId"
                :label="item.labelName"
                :value="item.labelId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签颜色" :label-width="formLabelWidth">
<!--            <el-input v-model="tag.colour" autocomplete="off"></el-input>-->
            <div class="block">
              <el-color-picker v-model="tag.colour"></el-color-picker>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="getAdd">确 定</el-button>
        </div>
      </el-dialog>
    </div>
    <el-button @click="delTag"><i class="el-icon-delete"></i>批量删除标签</el-button>
    <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="tagId" label="编号" show-overflow-tooltip></el-table-column>
      <el-table-column prop="labelName" label="标签分组" show-overflow-tooltip></el-table-column>
      <el-table-column prop="tagName"  label="标签名称" show-overflow-tooltip></el-table-column>
      <el-table-column prop="name" label="标签类型" show-overflow-tooltip></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row.tagId)" type="text" size="small"><i class="el-icon-edit"></i>查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchObj.pageNum"
      :page-sizes="[5, 10, 20, 100]"
      :page-size="searchObj.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {getAddTag, getByIdTag, getDelTag, getTagGroup, getTagList, getUpdTag} from "../../../api/monitor/manage";
import {MessageBox} from "element-ui";
  export default {
    data() {
      return {
        total: 0,
        tableData:[],
        searchObj:{
          tagName:'',
          labelId:'',
          pageNum:1,
          pageSize:20,
        },
        tag:{
          tagId:'',
          tagName:'',
          labelId:'',
          color1: '#409EFF',
        },
        multipleSelection: [],
        options:[],
        dialogFormVisible: false,
        formLabelWidth: '120px',
      }
    },
    created() {
      // 标签列表
      this.getList()
    // 标签分组
      this.getTagList()
    },
    methods:{
      //分页
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.searchObj.pageSize=val
        this.getList()
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.searchObj.pageNum=val
        this.getList()
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //标签列表展示
      getList() {
        getTagList(this.searchObj).then(response => {
          this.tableData = response.data.list;
          this.total=response.data.total
        })
      },
      // 标签分组
      getTagList() {
        getTagGroup().then(response => {
          this.options = response.data;
        })
      },
      // 添加标签
      getAdd() {
        if (this.tag.tagId){
          getUpdTag(this.tag).then(response => {
            this.$message.success('修改成功');
            this.getList()
            this.dialogFormVisible = false;
          })
        }else {
          getAddTag(this.tag).then(response => {
            this.$message.success('添加成功');
            this.getList()
            this.dialogFormVisible = false;
            this.tag = {
              tagName:'',
              labelId:'',
              color1: '',
            }
          })
        }
      },
      // 删除标签
      delTag() {
        if (this.multipleSelection.length===0){
          this.$message.warning("至少选则一个")
        }
        MessageBox.confirm('是否要删除', 'Confirm logout', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let  tagIds=[]
          this.multipleSelection.forEach(tag=>{
            tagIds.push(tag.tagId)
          })
          getDelTag(tagIds).then(res=>{
            if (tagIds.length!==0){
              this.$message.success("删除成功")
              this.getList()
            }
          })
        })
      },
      // 回显
      handleClick(tagId) {
        this.dialogFormVisible = true;
        getByIdTag(tagId).then(response => {
          this.tag = response.data;

        })
      },
    }
  }
</script>

<style scoped lang="scss">

</style>
